package xyz.hubery.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xyz.hubery.domain.SysPrivilege;
import xyz.hubery.model.R;
import xyz.hubery.service.SysPrivilegeService;

import java.util.Date;

@Api(tags = "权限管理")
@RestController
@RequestMapping("/privileges")
public class PrivilegeController {

    @Autowired
    private SysPrivilegeService privilegeService;

    @ApiOperation(value = "分页查询权限信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页"),
            @ApiImplicitParam(name = "size",value = "每页条数")
    })
    @GetMapping
    @PreAuthorize("hasAuthority('sys_privilege_query')")
    public R<Page<SysPrivilege>> findPage(Page<SysPrivilege> page) {
        page.addOrder(OrderItem.desc("last_update_time")); //修改后的放在前面
        Page<SysPrivilege> privilegePage = privilegeService.page(page);
        return R.ok(privilegePage);
    }

    @ApiOperation(value = "新增权限信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "权限名称"),
            @ApiImplicitParam(name = "description",value = "描述"),
            @ApiImplicitParam(name = "menu_id",value = "菜单id")
    })
    @PostMapping
    @PreAuthorize("hasAuthority('sys_privilege_create')")
    public R<String> addPrivilege(@RequestBody @Validated SysPrivilege sysPrivilege) {
        // 获取当前用户id
//        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
//        sysPrivilege.setCreateBy(Long.valueOf(userId));
//        sysPrivilege.setCreated(new Date());
//        sysPrivilege.setLastUpdateTime(new Date());
        boolean save = privilegeService.save(sysPrivilege);
        if(save) {
            return R.ok("成功");
        }
        return R.fail("失败");
    }


    @ApiOperation(value = "修改权限信息")
    @ApiImplicitParam(name = "sysPrivilege",value = "权限json数据")
    @PatchMapping
    @PreAuthorize("hasAuthority('sys_privilege_update')")
    public R<String> updatePrivilege(@RequestBody @Validated SysPrivilege sysPrivilege) {
        // 获取当前用户id
//        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
//        sysPrivilege.setModifyBy(Long.valueOf(userId));
//        sysPrivilege.setLastUpdateTime(new Date());
        boolean b = privilegeService.updateById(sysPrivilege);
        if(b) {
            return R.ok("成功");
        }
        return R.fail("失败");
    }



}
